<template>
  <div class="recommend">
    <div class="title">相关攻略</div>
    <div v-for="(item,index) in recommendList" :key="index">
      <nuxt-link :to="`/post/detail?id=${item.id}`">
        <div class="recommend_item">
          <div class="leftImg" v-if="item.images.length">
            <img :src="item.images" alt />
          </div>
          <div class="right">
            <div class="content">{{item.title}}</div>
            <div class="info">{{newDate(item)}} 阅读 {{item.watch}}</div>
          </div>
        </div>
      </nuxt-link>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      recommendList: []
    };
  },
  //   监听路由变化
  watch: {
    $route: {
      handler() {
        this.loadRecommend();
      },
      immediate: true
    }
  },
  //   mounted() {
  //     this.loadRecommend();
  //   },
  methods: {
    newDate(item) {
      return this.$moment(item.created_at).format("YYYY-MM-DD hh:mm");
    },
    // 获取相关攻略
    loadRecommend() {
      this.$axios({
        url: "/posts/recommend",
        params: {
          id: this.$route.query.id
        }
      }).then(res => {
        this.recommendList = res.data.data;
      });
    }
  }
};
</script>

<style lang="less" scoped>
.recommend {
  .title {
    font-size: 18px;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
  }
  .recommend_item {
    display: flex;
    padding: 20px 0;
    height: 80px;
    border-bottom: 1px solid #ddd;
    .leftImg {
      width: 100px;
      height: 80px;
      padding-right: 10px;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    .right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .content {
        font-size: 16px;
      }
      .info {
        font-size: 12px;
        color: #999;
      }
    }
    &:hover {
      cursor: pointer;
    }
  }
}
</style>